package com.zhouyibike.uu.ui.activity.mywallet;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetCodeResult;
import com.zhouyibike.uu.entity.VefityForUpdateResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.TextUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class ChongzhimimaActivity1 extends BaseActivity implements View.OnClickListener {
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private Button btn_logout, getyanzhengmabt;
    private EditText yanzhengma, shoujihao;
    private int resend = 60;//重写发生倒计时
    private MyHandler myHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chongzhimima1);
        myHandler = new MyHandler();
        initView();
        initListener();
    }

    private void initListener() {
        getyanzhengmabt.setOnClickListener(this);
        btn_logout.setOnClickListener(this);
    }

    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("验证身份");
        btn_logout = (Button) findViewById(R.id.btn_logout);
        getyanzhengmabt = (Button) findViewById(R.id.getyanzhengmabt);
        yanzhengma = (EditText) findViewById(R.id.yanzhengma);
        shoujihao = (EditText) findViewById(R.id.shoujihao);
        shoujihao.addTextChangedListener(new EditTextWatcher());
        yanzhengma.addTextChangedListener(new EditTextWatcher2());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.getyanzhengmabt:
                if (shoujihao.getText().toString().length() == 11) {
                    resend = 60;
                    getCode();
                } else {
                    toastShow("请输入正确的手机号");
                }
                break;
            case R.id.btn_logout:
                if (!TextUtils.isNullorEmpty(yanzhengma.getText().toString()) && !TextUtils.isNullorEmpty(shoujihao.getText().toString())) {
                    yanzhengCode();
                } else {
                    toastShow("手机号或验证码不能为空");
                }
                break;
        }
    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (resend <= 0) {
                getyanzhengmabt.setText("获取验证码");
                getyanzhengmabt.setEnabled(true);
            } else {
                getyanzhengmabt.setEnabled(false);
                getyanzhengmabt.setText("重新发送(" + resend + ")");
                resend--;
                myHandler.sendEmptyMessageDelayed(1, 1000);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == 1) {//重置密码成功
            setResult(1);
            finish();
        }
    }

    /**
     * 获取验证码
     */
    private void getCode() {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", shoujihao.getText().toString());
        String sign = shoujihao.getText().toString() + preferences.getString("appKey", "");
        params.put("token", preferences.getString("token", ""));
        params.put("sign", TextUtils.MD5(sign));
        showProgressDialog();
        addSubscription(apiStores.getVerifyCode(params),
                new ApiCallback<GetCodeResult>() {
                    @Override
                    public void onSuccess(GetCodeResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            myHandler.sendEmptyMessage(1);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }

    /**
     * 验证验证码
     */
    private void yanzhengCode() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("code", yanzhengma.getText().toString());
        showProgressDialog();
        addSubscription(apiStores.VefityForUpdate(params),
                new ApiCallback<VefityForUpdateResult>() {
                    @Override
                    public void onSuccess(VefityForUpdateResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                          //  toastShow(model.getMessage());
                            startActivityForResult(new Intent(ChongzhimimaActivity1.this, ChongzhimimaActivity2.class), 1001);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
    class EditTextWatcher implements TextWatcher {

        public void afterTextChanged(Editable edt) {

            //手机号不为空，且为11位
            if (!TextUtils.isNullorEmpty(shoujihao.getText().toString()) && Pattern.matches(Config.PHONEREGX, shoujihao.getText().toString())) {
                if(getyanzhengmabt.getText().toString().equals("获取验证码")){
                    getyanzhengmabt.setEnabled(true);
                }else{
                    getyanzhengmabt.setEnabled(false);
                }
                //验证码不为空
                if (!TextUtils.isNullorEmpty(yanzhengma.getText().toString())) {
                    btn_logout.setEnabled(true);  //提交按钮可点击
                }else {
                    btn_logout.setEnabled(false);  //提交按钮bu可点击
                }
            } else {
                btn_logout.setEnabled(false);  //提交按钮bu可点击
                getyanzhengmabt.setEnabled(false);
            }
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

    }
    class EditTextWatcher2 implements TextWatcher {

        public void afterTextChanged(Editable edt) {

            //手机号不为空，且为11位
            if (!TextUtils.isNullorEmpty(shoujihao.getText().toString()) && Pattern.matches(Config.PHONEREGX, shoujihao.getText().toString())) {
//                btn_getcode.setEnabled(true);
                //验证码不为空
                if (!TextUtils.isNullorEmpty(yanzhengma.getText().toString())) {
                    btn_logout.setEnabled(true);  //提交按钮可点击
                }else {
                    btn_logout.setEnabled(false);  //提交按钮bu可点击
                }
            } else {
                btn_logout.setEnabled(false);  //提交按钮bu可点击
            }
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

    }
}
